package com.summer.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.summer.content.dao.TeachPlanMapper;
import com.summer.content.dao.TeachplanMediaMapper;
import com.summer.commonException.XueChengException;
import com.summer.content.service.TeachPlanService;
import com.summer.dto.SaveTeachPlanDto;
import com.summer.dto.TeachPlanDto;
import com.summer.pojo.Teachplan;
import com.summer.pojo.TeachplanMedia;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class TeachPlanServiceImpl implements TeachPlanService {

    @Autowired
    private TeachPlanMapper teachPlanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachPlanDto> getCoursePlanById(Long courseId) {
        if (courseId == null){
            XueChengException.cast("课程id不能为null");
        }
        List<TeachPlanDto> coursePlanList = teachPlanMapper.getCoursePlanById(courseId);
        if (StringUtils.isEmpty(coursePlanList)){
            XueChengException.cast("id为"+ courseId + "的课程计划不存在");
        }
        return coursePlanList;
    }


    public void saveTeachPlan(SaveTeachPlanDto teachplanDto) {
        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if(id!=null){
            Teachplan teachplan = teachPlanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachPlanMapper.updateById(teachplan);
        }else{
            //取出同父同级别的课程计划数量
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            //设置排序号
            teachplanNew.setOrderby(count+1);
            BeanUtils.copyProperties(teachplanDto,teachplanNew);

            teachPlanMapper.insert(teachplanNew);

        }

    }


    @Transactional
    @Override
    public void deletePlan(Long id) {
        // 数据准备，查看删除大章节还是小章节
        Teachplan teachplan = teachPlanMapper.selectById(id);
        if (StringUtils.isEmpty(teachplan)){
            XueChengException.cast("id为" + id +"的课程计划不存在");
        }
        Integer grade = teachplan.getGrade();
        // 删除大章节, 有小章节时不能删除
        if (grade == 1){
            LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Teachplan::getParentid, id);
            Integer count = teachPlanMapper.selectCount(lambdaQueryWrapper);
            // 有小章节，不能删除
            if (count > 0){
                XueChengException.cast("大章节下有小章节，不能删除");
            }
            // 删除父节点
            int deleteParent = teachPlanMapper.deleteById(id);
            if (deleteParent <= 0){
                XueChengException.cast("删除课程计划失败，课程id = " + id);
            }
        }

        if (grade == 2){
            int deleteParent = teachPlanMapper.deleteById(id);
            if (deleteParent <= 0){
                XueChengException.cast("删除课程计划失败，课程id = " + id);
            }
            // 删除关联表
            LambdaQueryWrapper<TeachplanMedia> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(TeachplanMedia::getTeachplanId, id);
            int deleteRelate = teachplanMediaMapper.delete(lqw2);
            if (deleteRelate < 0){
                XueChengException.cast("删除失败");
            }
        }

    }

    @Transactional
    @Override
    public void movedown(Long id) {
        // 1. 数据准备：判断是否存在此记录
        Teachplan teachplanUp = teachPlanMapper.selectById(id);
        if (StringUtils.isEmpty(teachplanUp)){
            XueChengException.cast("下移失败！数据不存在");
        }
        // 逻辑校验，若为大章节则抛出异常
        if (teachplanUp.getGrade() == 1){
            XueChengException.cast("操作失败");
        }
        Integer orderbyUp = teachplanUp.getOrderby();

        // 1.1 查询 orderby+1 的数据 select * from teachplan where orderby = ? and parentid = ?
        LambdaQueryWrapper<Teachplan> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Teachplan::getOrderby, orderbyUp + 1)
           .eq(Teachplan::getParentid, teachplanUp.getParentid());
        Teachplan teachplanDown = teachPlanMapper.selectOne(lqw);
        if (StringUtils.isEmpty(teachplanDown)){
            XueChengException.cast("我是有底线的！！！");
        }

        // 2. 调用mapper修改
        // 2.1 修改up
        teachplanUp.setOrderby(orderbyUp + 1);
        int updateUp = teachPlanMapper.updateById(teachplanUp);
        if (updateUp <= 0){
            XueChengException.cast("下移失败！");
        }
        // 2.2 修改down
        teachplanDown.setOrderby(teachplanDown.getOrderby() - 1);
        int updateDown = teachPlanMapper.updateById(teachplanDown);
        if (updateDown <= 0){
            XueChengException.cast("下移失败！！！");
        }


    }

    @Override
    public void moveup(Long id) {
        // 1. 数据准备：判断是否存在此记录
        Teachplan teachplanDown = teachPlanMapper.selectById(id);
        if (StringUtils.isEmpty(teachplanDown)){
            XueChengException.cast("上移失败！数据不存在");
        }
        // 逻辑校验
        if (teachplanDown.getGrade() == 1){
            XueChengException.cast("操作失败");
        }
        Integer orderbyDown = teachplanDown.getOrderby();

        // 1.1 查询 orderby-1 的数据 select * from teachplan where orderby = ? and parentid = ?
        LambdaQueryWrapper<Teachplan> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Teachplan::getOrderby, orderbyDown - 1)
                .eq(Teachplan::getParentid, teachplanDown.getParentid());
        Teachplan teachplanUp = teachPlanMapper.selectOne(lqw);
        if (StringUtils.isEmpty(teachplanUp)){
            XueChengException.cast("我是有上限的！！！");
        }

        // 2. 调用mapper修改
        // 2.1 修改down
        teachplanDown.setOrderby(orderbyDown - 1);
        int updateDown = teachPlanMapper.updateById(teachplanDown);
        if (updateDown <= 0){
            XueChengException.cast("下移失败！");
        }
        // 2.2 修改down
        teachplanUp.setOrderby(teachplanUp.getOrderby() + 1);
        int updateUp = teachPlanMapper.updateById(teachplanUp);
        if (updateUp <= 0){
            XueChengException.cast("下移失败！！！");
        }

    }

    /**
     * @description 获取最新的排序号
     * @param courseId  课程id
     * @param parentId  父课程计划id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        return teachPlanMapper.selectCount(queryWrapper);
    }

}
